<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="40%">
    <el-form ref="formItem" :model="formItem" label-width="80px" size="small">
      <el-row>
        <el-col :span="12">
          <el-form-item label="客户编号" prop="custId">
            <el-input v-model="formItem.custId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="name">
            <el-input v-model="formItem.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户简称" prop="simpleNm">
            <el-input v-model="formItem.simpleNm"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联络人" prop="contact">
            <el-input v-model="formItem.contact"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="等级" prop="rank">
            <el-select v-model="formItem.rank" placeholder="请选择">
              <el-option label="客户" :value="1" />
              <el-option label="经销商" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联络电话1" prop="phone1">
            <el-input v-model="formItem.phone1"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结算方式" prop="balance">
            <el-select v-model="formItem.balance" placeholder="请选择">
              <el-option label="信用" :value="1" />
              <el-option label="现结" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联络电话2" prop="phone2">
            <el-input v-model="formItem.phone2"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款方式" prop="payMethod">
            <el-select v-model="formItem.payMethod" placeholder="请选择">
              <el-option label="银行转账" :value="1" />
              <el-option label="银行承兑" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { modalFormMixin } from '@/utils/mixin'
import { addCust, updateCust } from '@/api/cust'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      formItem: {
        custId: '',
        name: '',
        rank: 2,
        simpleNm: '',
        contact: '',
        phone1: '',
        phone2: '',
        balance: 2,
        payMethod: 1
      }
    }
  },
  mixins: [modalFormMixin],
  methods: {
    handleSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = this.isEdit ? await updateCust(this.formItem) : await addCust(this.formItem)
          if (res.code === 200) {
            this.$emit('ok')
            this.dialogVisible = false
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    }
  }
}
</script>

<style>
</style>